package com.doge.service.admin.region;

import com.doge.bean.base.BaseData;
import com.doge.bean.pojo.region.Region;
import com.doge.bean.pojo.region.RegionExample;
import com.doge.bean.vo.region.*;
import com.doge.mapper.RegionMapper;
import com.doge.util.anno.FuncTimeCount;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Mr.Jiang
 * @create: 2021-11-29 21:41
 **/
@Service
public class RegionServiceImpl implements RegionService{

    @Autowired
    RegionMapper regionMapper;

    @FuncTimeCount
    @Override
    public RegionResultVo list() {

        ArrayList<ProvinceForRegionResultVo> provinceList = new ArrayList<>();
        //   // 在执行查询前开启分页
        //   PageHelper.startPage(Param.getPage(), param.getLimit());
        //   // 通过 分页信息PageInfo 得到分页数据: 有参构造 放入查询结果
        //   new PageInfo(users).getTotal();  //获取总的分页信息
        RegionExample regionExample = new RegionExample();
        regionExample.createCriteria().andTypeEqualTo(((byte) 1));
        //查省数量
        long count = regionMapper.countByExample(regionExample);

        List<Region> regionsForProvince = regionMapper.selectByExample(regionExample);
        //省循环
        for (Region regionsForP : regionsForProvince) {


            //查市
            RegionExample regionExampleSavePid = new RegionExample();
            regionExampleSavePid.createCriteria().andPidEqualTo(regionsForP.getId());
            List<Region> regionForCity = regionMapper.selectByExample(regionExampleSavePid);
            ArrayList<CityForProvinceForRegionResultVo> cityList = new ArrayList<>();
            //市循环
            for (Region regionForC : regionForCity) {

                //查区域,查完将一个市和区域集合加入市
                RegionExample regionExampleSaveCid = new RegionExample();
                regionExampleSaveCid.createCriteria().andPidEqualTo(regionForC.getId());
                List<Region> regionForDistrict = regionMapper.selectByExample(regionExampleSaveCid);

                ArrayList<DistrictForCityForProvinceForRegionResultVo> districtList = new ArrayList<>();
                //区域循环
                for (Region region : regionForDistrict) {
                    DistrictForCityForProvinceForRegionResultVo districtForCityForProvinceForRegionResultVo = new DistrictForCityForProvinceForRegionResultVo();
                    districtForCityForProvinceForRegionResultVo.setId(region.getId());
                    districtForCityForProvinceForRegionResultVo.setName(region.getName());
                    districtForCityForProvinceForRegionResultVo.setType((int)region.getType());
                    districtForCityForProvinceForRegionResultVo.setCode(region.getCode());
                    districtList.add(districtForCityForProvinceForRegionResultVo);
                }

                CityForProvinceForRegionResultVo cityForProvinceForRegionResultVo = new CityForProvinceForRegionResultVo();
                cityForProvinceForRegionResultVo.setId(regionForC.getId());
                cityForProvinceForRegionResultVo.setName(regionForC.getName());
                cityForProvinceForRegionResultVo.setType((int)regionForC.getType());
                cityForProvinceForRegionResultVo.setCode(regionForC.getCode());
                cityForProvinceForRegionResultVo.setChildren(districtList);
                cityList.add(cityForProvinceForRegionResultVo);
            }
            //查完一个省将其加入省集合
            ProvinceForRegionResultVo provinceForRegionResultVo = new ProvinceForRegionResultVo();
            provinceForRegionResultVo.setId(regionsForP.getId());
            provinceForRegionResultVo.setName(regionsForP.getName());
            provinceForRegionResultVo.setType((int) regionsForP.getType());
            provinceForRegionResultVo.setCode(regionsForP.getCode());
            provinceForRegionResultVo.setChildren(cityList);
            provinceList.add(provinceForRegionResultVo);
        }
        RegionResultVo resultVo = new RegionResultVo();
        resultVo.setTotal((int) count);
        resultVo.setPages(1);
        resultVo.setLimit((int) count);
        resultVo.setPage(1);
        resultVo.setList(provinceList);
        return resultVo;
    }

    @FuncTimeCount
    @Override
    public BaseData list2() {

        // 查询得到所有的region
        List<Region> regions = regionMapper.selectByExample(new RegionExample());
        // 将 region 转换为 province，city，district
        ArrayList<Province> provinces = new ArrayList<>();
        for (Region proR : regions) {
            // 省
            if (proR.getType()==1){
                Province province = new Province();
                province.setId(proR.getId());
                province.setName(proR.getName());
                province.setType((byte)1);
                province.setCode(proR.getCode());
                // 市
                List<Province.City> cities = new ArrayList<>();
                for (Region cityR : regions) {
                    if (cityR.getPid().equals(proR.getId())){
                        Province.City city = new Province.City();
                        city.setId(cityR.getId());
                        city.setName(cityR.getName());
                        city.setType((byte)2);
                        city.setCode(city.getCode());
                        // 县
                        List<Province.City.District> districts = new ArrayList<>();
                        for (Region disR : regions) {
                            if (disR.getPid().equals(cityR.getId())){
                                Province.City.District dis = new Province.City.District();
                                dis.setId(disR.getId());
                                dis.setName(disR.getName());
                                dis.setType((byte)3);
                                dis.setCode(disR.getCode());
                                districts.add(dis);
                            }
                        }
                        city.setChildren(districts);
                        cities.add(city);
                    }
                }
                province.setChildren(cities);
                provinces.add(province);
            }
        }
        // 分页信息是写死的
        int provinceCount = provinces.size();
        return new BaseData(1,provinceCount,(long)provinceCount,1,provinces);
    }
}
